<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="支付时间" name="payTime">
						<a-range-picker v-model:value="searchFormState.payTime" show-time value-format="YYYY-MM-DD HH:mm:ss" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'" />
					</a>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
			:showPagination="false"
		>
			<template #operator class="table-operator">
				<a-button @click="exportBatchUserVerify">
					<template #icon><export-outlined /></template>
					导出
				</a-button>
			</template>

			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'cafeteriaType'">
					{{ $TOOL.dictTypeData('CAFETERIA_TYPE', record.cafeteriaType) }}
				</template>
				<template v-if="column.dataIndex === 'payType'">
					{{ $TOOL.dictTypeData('PAY_TYPE', record.payType) }}
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="approvalorder">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import bizApprovalOrderApi from '@/api/biz/bizApprovalOrderApi'
	import tool from '@/utils/tool'
	import dayjs from 'dayjs'
	const searchFormState = ref({
		payTime: [dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'), dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss')]
	})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }

	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '部门ID',
			dataIndex: 'applicantDeptId'
		},
		{
			title: '部门',
			dataIndex: 'applicantDeptName'
		},
		{
			title: '订单数',
			dataIndex: 'totalOrders'
		},
		{
			title: '餐食总费用',
			dataIndex: 'totalCafeteriaPrice'
		}
	]
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	let loadDataSWith = true

	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)

		// payTime范围查询条件重载
		if (searchFormParam.payTime) {
			searchFormParam.startPayTime = searchFormParam.payTime[0]
			searchFormParam.endPayTime = searchFormParam.payTime[1]
			delete searchFormParam.payTime
		}
		return bizApprovalOrderApi.deptList(Object.assign(parameter, searchFormParam)).then((data) => {
			console.log(data)
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	const cafeteriaTypeOptions = tool.dictList('CAFETERIA_TYPE')
	const payTypeOptions = tool.dictList('PAY_TYPE')
	async function exportBatchUserVerify() {
		const searchFormParam = cloneDeep(searchFormState.value)

		if (searchFormParam.payTime) {
			searchFormParam.startPayTime = searchFormParam.payTime[0]
			searchFormParam.endPayTime = searchFormParam.payTime[1]
			delete searchFormParam.payTime
		}

		const response = await bizApprovalOrderApi.exportDeptList(searchFormParam)
		const base64Data = response.body
		const contentDisposition = response.headers['Content-Disposition'][0]
		const fileNameMatch = contentDisposition.match(/filename="?([^"]+)"?/)
		const filename = fileNameMatch ? fileNameMatch[1] : '部门报表.xlsx'

		// Base64 to Blob

		const blob = base64ToBlob(base64Data)

		// 创建链接并下载
		const url = window.URL.createObjectURL(blob)
		const link = document.createElement('a')
		link.href = url
		link.setAttribute('download', filename)
		document.body.appendChild(link)
		link.click()
		link.remove()
		window.URL.revokeObjectURL(url)
	}
	function base64ToBlob(base64, mimeType = 'application/octet-stream') {
		const byteString = atob(base64)
		const ab = new ArrayBuffer(byteString.length)
		const ia = new Uint8Array(ab)
		for (let i = 0; i < byteString.length; i++) {
			ia[i] = byteString.charCodeAt(i)
		}
		return new Blob([ab], { type: mimeType })
	}
</script>
